using System;
using System.Collections.Generic;
using System.Linq;
using LastFmLib.API20;
using LastFmLib.API20.User;
using StaffWarsFm.Lib.Data;

namespace StaffWarsFm.Questions
{
    public class BiggestFanQuestionService : QuestionServiceBase
    {
        [Question]
        public Question GetBiggestFanOfAlbum(IEnumerable<User> players, IEnumerable<User> allUsers)
        {
            var user = GetUser(players, allUsers);   
            var getUserInfo = new UserGetTopAlbums(user.UserName, modEnums.ChartPeriod.TwelveMonths);
            
            getUserInfo.AuthData = Util.GetAuthData();
            getUserInfo.Start();

            if (getUserInfo.Succeeded)
            {
                var album = getUserInfo.Result[0];

                var question = new Question 
                {
                    Text = string.Format("Who is a big fan of the album {0}?", album.AlbumName),
                    ImageUrl = album.ImageLarge.ToString(),
                    Answers = GetAnswers(
                        user,
                        a => new Answer 
                        {
                            Text = a.Default(r => a.DisplayName, ""),
                            ImageUrl = ""
                        }
                    )
                };

                return question;
            }

            throw new Exception("Your fucked now! have a shot");   
        }

        [Question]
        public Question GetBiggestFanArtist(IEnumerable<User> players, IEnumerable<User> allUsers)
        {
            var user = GetUser(players, allUsers);
            var getUserInfo = new UserGetTopArtists(user.UserName, modEnums.ChartPeriod.TwelveMonths);

            getUserInfo.AuthData = Util.GetAuthData();
            getUserInfo.Start();

            if (getUserInfo.Succeeded)
            {
                var artist = getUserInfo.Result[0];

                var question = new Question 
                {
                    Text = string.Format("Who is a big fan of the band {0}?", artist.Name),
                    ImageUrl = artist.ImageLarge.ToString(),
                    Answers = GetAnswers(
                        user,
                        a => new Answer 
                        {
                            Text = a.Default(r => r.DisplayName, ""),
                            ImageUrl = ""
                        }
                    )
                };

                return question;
            }

            throw new Exception("Ive eaten too much pizza");   
        }

        private User GetUser(IEnumerable<User> players, IEnumerable<User> allUsers)
        {
            var users = allUsers.Where(u => false == players.Contains(u)).ToArray();
            return users[Util.Random(users.Length)];
        }
    }
}